﻿using System;
using System.Configuration;
using System.Linq;
using System.Web;
using EOSCRM.Domain;
using EOSCRM.Util;

namespace EOSCRM.Dao
{
    public class UserLogDao:BaseDao
    {
        public string SessionIdentify = "_EL_USER";
        

        public UserLog Get(int logID)
        {
            return _db.UserLogs.SingleOrDefault(w => w.Id == logID);
        }
        public UserLog Get(string sessionID, DateTime gDate)
        {
            return _db.UserLogs.SingleOrDefault(w => w.SessionID == sessionID && w.MANV == "" && w.LogDate == gDate);
        }
        public UserLog Get(string sessionID, string logUser, DateTime gDate)
        {
            return _db.UserLogs.SingleOrDefault(w => w.SessionID == sessionID && w.MANV == logUser && w.LogDate == gDate);
        }

        public UserLog New() { return New(""); }
        public UserLog New(string logUser)
        {
            var newRecord = new UserLog();
            try
            {
                var curDate = DateTime.Now;
                var curSessionID = HttpContext.Current.Session.SessionID;
                var oldLog = string.IsNullOrEmpty(logUser) ? 
                                        Get(curSessionID, curDate) : 
                                        Get(curSessionID, logUser, curDate);
                if (oldLog != null)
                {
                    oldLog.LogCount++;
                    oldLog.LastAlive = curDate.TimeOfDay;
                    _db.SubmitChanges();
                    return oldLog;
                }

                var curRequest = HttpContext.Current.Request;
                newRecord.ClientIP = curRequest.Params["REMOTE_ADDR"];
                newRecord.MANV = logUser;
                newRecord.LogDate = curDate.Date;
                newRecord.LogTime = curDate.TimeOfDay;
                newRecord.LogCount = Convert.ToInt16(string.IsNullOrEmpty(logUser) ? 0 : 1);
                newRecord.SessionID = curSessionID;
                newRecord.Browser = curRequest.Browser.Browser;
                newRecord.Version = curRequest.Browser.Version;
                newRecord.FlatForm = curRequest.Browser.Win32 ? "x86" : "x64";

                _db.UserLogs.InsertOnSubmit(newRecord);
                _db.SubmitChanges();
            }
            catch { }

            return newRecord;
        }

        public bool UpdateAliveTime()
        {
            try
            {
                var objCL = (HttpContext.Current.Session[SessionIdentify] as UserLog);
                if (objCL != null)
                {
                    var objUpdate = Get(objCL.Id);
                    if (objUpdate != null)
                    {
                        objUpdate.LastAlive = DateTime.Now.TimeOfDay;
                        _db.SubmitChanges();
                    }
                }
            }
            catch { return false; }
            return true;
        }

        /*
        protected UserAdmin curUser = null;
        public List<el_userLog_LIST> usersLog() { return usersLog(true); }
        public List<el_userLog_LIST> usersLog(bool isOnline)
        {
            int minuteTimeout = HttpContext.Current.Session.Timeout;
            if (curUser == null) curUser = new UserAdminDao().CurrentUser();
            var urList = _db.el_userLog_LISTs.Where(w =>
                                                        w.LogDate == DateTime.Now.Date
                                                        && (
                                                            (isOnline && w.AliveTime <= minuteTimeout)
                                                            || ((!isOnline) && w.AliveTime > minuteTimeout)
                                                        )
                                                   ).ToList();
            if (curUser.MANV.ToLower() != "admin") urList = urList.Where(w => w.maKV == curUser.NHANVIEN.MAKV).ToList();

            return urList.OrderByDescending(o => o.maKV + o.maNV + o.LogTime.ToString()).ToList();
        }
        public List<el_userLog_LIST> usersLog(string maKV)
        {
            return usersLog().Where(w => w.maKV == maKV).ToList();
        }
        public List<el_userLog_LIST> usersLog(string maKV, bool isOnline)
        {
            return usersLog(isOnline).Where(w => w.maKV == maKV).ToList();
        }
        public List<el_userLog_KV> usersLog_KhuVuc()
        {
            int minuteTimeout = HttpContext.Current.Session.Timeout;
            if (curUser == null) curUser = new UserAdminDao().CurrentUser();
            var urList = _db.el_userLog_KVs.ToList();
            if (curUser.MANV.ToLower() != "admin") urList = urList.Where(w => w.maKV == curUser.NHANVIEN.MAKV).ToList();
            for (int i = urList.Count - 1; i >= 0; i--) if (usersLog(urList[i].maKV).Count == 0) urList.RemoveAt(i);
            return urList.OrderBy(o => o.maKV).ToList();
        }
        */
    }
}
